#version 330 core
struct Material{
  sampler2D diffuse;
  sampler2D specular;
  float shininess;
};

struct Light{
                //vec3 position;
  vec3 direction;//定向光源

  vec3 ambient;
  vec3 diffuse;
  vec3 specular;
};

out vec4 FragColor;
in vec2 TexCoords;

uniform Material material;
uniform Light light;

uniform vec3 viewPos;

in vec3 Normal;
in vec3 FragPos;

void main()
{
    //ambient
    vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, TexCoords));

    //diffuse
    vec3 norm = normalize(Normal);
    //vec3 lightDir = normalize(light.position - FragPos);
    vec3 lightDir = normalize(-light.direction); // 我们需要对全局光照方向向量取反来改变它的方向，它现在是一个指向光源的方向向量了
    float diff = max(dot(norm, lightDir), 0.0f);
    vec3 diffuse = light.diffuse * (diff * vec3(texture2D(material.diffuse, TexCoords)));

    //specular
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
    vec3 specular = light.specular * (spec * vec3(texture2D(material.specular, TexCoords)));

    //all
    vec3 result =  ambient + diffuse + specular;
    FragColor = vec4(result, 1.0f);
}
